package com.example.auv_660.network.tcp;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class ModuleReadHandler implements CompletionHandler<Integer, ByteBuffer> {
    private static final int BUFFER_LENGTH = 1024;
    private AsynchronousSocketChannel asynchronousSocketChannel;

    public ModuleReadHandler(AsynchronousSocketChannel asynchronousSocketChannel) {
        this.asynchronousSocketChannel = asynchronousSocketChannel;
    }

    @Override
    public void completed(Integer result, ByteBuffer attachment) {
    }

    @Override
    public void failed(Throwable exc, ByteBuffer attachment) {
        exc.printStackTrace();
    }

    public void getMessageFromModule() {
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH);
        if (this.asynchronousSocketChannel != null) {
            this.asynchronousSocketChannel.read(buffer, buffer, this);
        }
    }
}
